/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */

package com.boots.easypoi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.module.boots.api.message.ResponseMsg;
import com.module.boots.api.utils.MsgUtils;
import com.module.boots.easypoi.ExcelExportUtils;
import com.module.boots.easypoi.ExcelImportUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * excel导入导出
 * @author：溪云阁
 * @date：2020年5月29日
 */
@Api(tags = { "APP服务：数据接口" })
@RestController
@RequestMapping("view/ie")
public class ImportExportController {

    @Autowired
    private ExcelExportUtils excelExportUtils;

    @Autowired
    private ExcelImportUtils excelImportUtils;

    /**
     * 导出用户信息
     * @author 溪云阁 void
     */
    @ApiOperation(value = "导出excel")
    @GetMapping(value = "/exportExcel")
    public void exportExcel() throws Exception {
        final List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            final User user = new User();
            user.setUserId(i);
            user.setSex(1);
            user.setMoney(12332123 + i);
            user.setUserName("小明" + i);
            user.setPrice(23.1f + i);
            user.setNow(new Date());
            userList.add(user);
        }
        excelExportUtils.exportExcel(userList, User.class, "用户信息", "员工信息的sheet", "用户信息表");
    }

    /**
     * 导入用户信息
     * @author 溪云阁
     * @param file
     * @return
     * @throws IOException Object
     */
    @ApiOperation(value = "导入excel")
    @GetMapping(value = "/importExcel")
    public ResponseMsg<List<User>> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        final List<User> userList = excelImportUtils.importExcel(file, 1, 1, false, User.class);
        return MsgUtils.buildSuccessMsg(userList);
    }

}
